package com.jy.api.service.impl;

import com.jy.api.dao.ClerkSaleDao;
import com.jy.api.dao.ProductTypeDao;
import com.jy.api.domain.ProductType;
import com.jy.api.service.SaleService;
import com.jy.api.vo.clerk.SaleInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2018/3/24.
 */
@Service
public class SaleServiceImpl implements SaleService {


    @Autowired
    ClerkSaleDao saleDao;

    @Autowired
    ProductTypeDao productTypeDao;


    public SaleInfoVo querySaleInfo(String storeId, String username) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int upMonth = localDateTime.minusMonths(1).getMonthValue();
        int month = localDateTime.getMonthValue();

        Map<String, ProductType> typeMap = querStoreProductTypeAll(storeId);
        typeMap.put("1", productTypeDao.findOne("1"));
        SaleInfoVo saleInfoVo = new SaleInfoVo(saleDao.queryMonthTotal(username, storeId, upMonth));
        List<Map<String, Object>> typeCounts = saleDao.queryTypeCount(username, storeId, month);
        for (Map<String, Object> map : typeCounts) {
            ProductType type = null;
            if (map.get("productTypeId") != null) {
                type = typeMap.get(map.get("productTypeId").toString());
            }
            saleInfoVo.setClassifyCount(map, type);
        }
        return saleInfoVo;
    }

    //获取店铺所有分类信息
    private Map<String, ProductType> querStoreProductTypeAll(String storeId) {
        return productTypeDao.findByStoreId(storeId).stream().
                collect(Collectors.toMap(ProductType::getId, type -> type));
    }
}
